/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.gruposicom.dao.implement;

import ar.com.gruposicom.dao.PoliticDao;
import ar.com.gruposicom.domain.Politic;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author marcelo
 */
public class PoliticDaoImp extends DataManager implements PoliticDao
{
    private Statement stmt;
    private Connection conn;

    @Override
    public int save(Politic p, int idCommand) 
    {
        ResultSet resu = null;
        int id = 0;
        try 
        { 
            conn = super.getConection();
            stmt = conn.createStatement();
            stmt.executeUpdate("INSERT INTO politicas (comando_id, oracion, objetivo, nombre_parametro, cadena_inicio, cadena_fin, nombre_politica) VALUES ("
            +idCommand+",'"+p.getSentence()+"','"+p.getObjetive()+"', '"+p.getParamName()+"', '"+p.getInitString()+"', '"+p.getEndString()+"', "
                    + "'"+p.getName().trim().toUpperCase()+"')");  
            
            resu = stmt.executeQuery("Select Max(politica_id) From politicas");
            
        }
        catch (SQLException ex)
        {
          while (ex !=null)
            {
            ex.printStackTrace();
            ex = ex.getNextException();
            }
        }
        try
        {
            resu.next();
            id = resu.getInt(1);
            this.cerrar();
        }
        catch(SQLException ex)
        {
            Logger.getLogger(OperationDaoImp.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }

    @Override
    public Collection<Politic> getAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Politic get(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void delete(Politic p) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void modify(Politic p) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Collection<Politic> getAllFromCommand(int idCommand) 
    {
        ResultSet resu = null;
        try 
        { 
            conn = super.getConection();      
            stmt = conn.createStatement();  
            resu = stmt.executeQuery("SELECT politica_id, oracion, objetivo, nombre_parametro, cadena_inicio, cadena_fin, nombre_politica "
                    + "FROM politicas "
                    + "WHERE comando_id = "+idCommand); 
        }
        catch (SQLException ex)
        {
            while (ex !=null)
            {
                ex.printStackTrace();
                ex = ex.getNextException();
            }
        } 
        Collection<Politic> co = new ArrayList<>();   
        try
        {
            while (resu.next())
            {
                Politic p = new Politic();
                p.setId(resu.getInt(1));
                p.setSentence(resu.getString(2));
                p.setObjetive(resu.getString(3));
                p.setParamName(resu.getString(4));
                p.setInitString(resu.getString(5));
                p.setEndString(resu.getString(6));
                p.setName(resu.getString(7));
                co.add(p);
            }
        }
        catch(SQLException ex)
        {
            while (ex !=null)
            {
                ex.printStackTrace();
                ex = ex.getNextException();
            }
        }
        this.cerrar();            
        return co; 
    }
    
    private void cerrar()
    {
      try
      {
        stmt.close();
        conn.close();
      }
      catch(SQLException e)
      {
        while (e != null) 
        {
          e.printStackTrace();
          e = e.getNextException();
        }        
      }      
    }
    
}
